function webpad() {
	this.wadoURL;
	this.mouseLocX1;
	this.mouseLocX2;
	this.mouseLocY1;
	this.mouseLocY2;
	this.wc;
	this.ww;
	this.wcenter;
	this.wwidth;
	this.rescale_Slope;
	this.rescale_Intercept;
	this.lookupTable;
	this.huLookupTable;
	this.pixelBuffer = new Array(512 * 512 * 2);
	this.row = 512;
	this.column = 512;
	this.lookupObj;
	this.mousePressed = 0;

	this.canvas;
	this.imageLoaded = 0;
	this.ctx;
	this.myImageData;
	this.canvasElementId = "canvas";

	// start functions
	this.mouseDownHandler = mouseDownHandler;
	this.mouseDownHandler2 = mouseDownHandler2;
	
	this.mouseupHandler = mouseupHandler;
	this.mouseupHandler2 = mouseupHandler2;

	this.mousemoveHandler = mousemoveHandler;
	this.mousemoveHandler2 = mousemoveHandler2;
	
	this.showHUvalue = showHUvalue;
	this.showWindowingValue = showWindowingValue;
	this.loadDicom = loadDicom;
	this.getContextPath = getContextPath;
	this.parseAndLoadDicom = parseAndLoadDicom;
	this.genImage = genImage;

	String.prototype.replaceAll = function(pcFrom, pcTo) {
		var i = this.indexOf(pcFrom);
		var c = this;
		while (i > -1) {
			c = c.replace(pcFrom, pcTo);
			i = c.indexOf(pcFrom);
		}
		return c;
	}
	function mouseDownHandler(evt) {
		this.mousePressed = 1;
		if (this.imageLoaded == 1) {
			this.mouseLocX2 = evt.pageX - getPosicaoElemento(this.canvasElementId).left;
			this.mouseLocY2 = evt.pageY - getPosicaoElemento(this.canvasElementId).top;
		}
	}
	function mouseDownHandler2(x,y) {
		this.mousePressed = 1;
		if (this.imageLoaded == 1) {
			this.mouseLocX2 = x;
			this.mouseLocY2 = y;
		}
	}
	function mouseupHandler(evt) {
		this.mousePressed = 0;
	}
	function mouseupHandler2() {
		this.mousePressed = 0;
	}
	function mousemoveHandler(evt) {
		try {
			if (this.imageLoaded == 1) {
				this.mouseLocX1 = evt.pageX - getPosicaoElemento(this.canvasElementId).left;
				this.mouseLocY1 = evt.pageY - getPosicaoElemento(this.canvasElementId).top;
				if (this.mouseLocX1 >= 0 && this.mouseLocY1 >= 0 && this.mouseLocX1 < this.column && this.mouseLocY1 < this.row) {
					this.showHUvalue(this.mouseLocX1, this.mouseLocY1);
					if (this.mousePressed == 1) {
						this.imageLoaded = 0;
						var diffX = this.mouseLocX1 - this.mouseLocX2;
						var diffY = this.mouseLocY2 - this.mouseLocY1;
						this.wc = parseInt(this.wc) + diffY;
						this.ww = parseInt(this.ww) + diffX;
						this.showWindowingValue(this.wc, this.ww);
						this.lookupObj.setWindowingdata(this.wc, this.ww);
						this.genImage();
						this.mouseLocX2 = this.mouseLocX1
						this.mouseLocY2 = this.mouseLocY1;
						this.imageLoaded = 1;
					}
				}
			}
		} catch (err) {
			alert(err);
		}
	}
	function mousemoveHandler2(x,y) {
		try {
			if (this.imageLoaded == 1) {
				this.mouseLocX1 = x;
				this.mouseLocY1 = y;
				if (this.mouseLocX1 >= 0 && this.mouseLocY1 >= 0 && this.mouseLocX1 < this.column && this.mouseLocY1 < this.row) {
					if (this.mousePressed == 1) {
						this.imageLoaded = 0;
						var diffX = this.mouseLocX1 - this.mouseLocX2;
						var diffY = this.mouseLocY2 - this.mouseLocY1;
						this.wc = parseInt(this.wc) + diffY;
						this.ww = parseInt(this.ww) + diffX;
						//this.showWindowingValue(this.wc, this.ww);
						this.lookupObj.setWindowingdata(this.wc, this.ww);
						this.genImage();
						this.mouseLocX2 = this.mouseLocX1
						this.mouseLocY2 = this.mouseLocY1;
						this.imageLoaded = 1;
					}
				}
			}
		} catch (err) {
			alert(err);
		}
	}
	function showHUvalue(x, y) {
		var t = (y * this.column) + x;
		var hupanelx = document.getElementById("huDisplayPanelx");
		var hupanely = document.getElementById("huDisplayPanely");
		var hupanelh = document.getElementById("huDisplayPanelhu");
		hupanelx.innerHTML = x;
		hupanely.innerHTML = y;
		hupanelh.innerHTML = this.huLookupTable[this.pixelBuffer[t]];
	}
	function showWindowingValue(wcenter, wwidth) {
		var windowingPanelc = document.getElementById("windowingPanel_center");
		var windowingPanelw = document.getElementById("windowingPanel_width");
		windowingPanelc.innerHTML = wcenter;
		windowingPanelc.innerHTML = wwidth;
	}
	function loadDicom(uri) {
		this.wadoURL = uri;
		this.parseAndLoadDicom();
	}
	function getContextPath() {
		var path = top.location.pathname;
		if (document.all) {
			path = path.replace(/\\/g, "/");
		}
		path = path.substr(0, path.lastIndexOf("/") + 1);
		path = path.replace("/", "");
		return path;
	}
	function parseAndLoadDicom() {
		var reader = new DicomInputStreamReader();
		//reader.readDicom("http://" + top.location.host + "/web_pad/" + getContextPath() + "dcmstream?wadourl=" + this.wadoURL.replaceAll("&", "-"));
		reader.readDicom(this.wadoURL);
		var dicomBuffer = reader.getInputBuffer();
		var dicomReader = reader.getReader();
		var dicomParser = new DicomParser(dicomBuffer, dicomReader);
		dicomParser.parseAll();
		var elementindex = 0;
		for (; elementindex < dicomParser.dicomElement.length; elementindex++) {
			if (dicomParser.dicomElement[elementindex]) {
				var dicomElement = dicomParser.dicomElement[elementindex];
				if (dicomElement.name == "windowWidth") {
					this.wwidth = this.ww = parseInt(dicomElement.value[0]);
				} else if (dicomElement.name == "windowCenter") {
					this.wcenter = this.wc = parseInt(dicomElement.value[0]);
				} else if (dicomElement.name == "rescaleIntercept") {
					this.rescale_Intercept = parseInt(dicomElement.value);
				} else if (dicomElement.name == "rescaleSlope") {
					this.rescale_Slope = parseInt(dicomElement.value);
				}
			}
		}
		this.pixelBuffer = dicomParser.pixelBuffer;
		this.lookupObj = new LookupTable();
		this.lookupObj.setData(this.wc, this.ww, this.rescale_Slope,
				this.rescale_Intercept);
		this.lookupObj.calculateHULookup();
		this.huLookupTable = this.lookupObj.huLookup;
		this.canvas = document.getElementById(this.canvasElementId);
		this.ctx = this.canvas.getContext("2d");
		this.ctx.fillRect(0, 0, 512, 512);
		this.myImageData = this.ctx.getImageData(0, 0, 512, 512);
		this.genImage();
		this.imageLoaded = 1;
	}
	function genImage() {
		this.lookupObj.calculateLookup();
		this.lookupTable = this.lookupObj.ylookup;
		var n = 0;
		for ( var yPix = 0; yPix < this.row; yPix++) {
			for ( var xPix = 0; xPix < this.column; xPix++) {
				var offset = (yPix * this.column + xPix) * 4;
				var pxValue = this.lookupTable[this.pixelBuffer[n]];
				n++;
				this.myImageData.data[offset] = parseInt(pxValue);
				this.myImageData.data[offset + 1] = parseInt(pxValue);
				this.myImageData.data[offset + 2] = parseInt(pxValue);
			}
		}
		this.ctx.putImageData(this.myImageData, 0, 0);
	}
}